package com.prestamomo.vil.ppa.pmvilmedium.pmvilview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class PmvilLimitView extends View {
    private Paint pmvilWyPainth = new Paint();
    private Paint pmvilWyPaintw = new Paint();
    private Paint pmvilNyPaint1 = new Paint();
    private Paint pmvilNyPaint2 = new Paint();

    private int DEFAULT_WIDTH = 100;
    private int DEFAULT_HEIGHT = 100;

    private float _angle;
    public PmvilLimitView(Context context) {
        super(context);
        pmvilInit();
    }

    public PmvilLimitView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        pmvilInit();
    }

    public PmvilLimitView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        pmvilInit();
    }

    private void pmvilInit() {

        pmvilWyPainth.setFlags(Paint.ANTI_ALIAS_FLAG);
        pmvilWyPainth.setColor(Color.parseColor("#FF7B32"));
        pmvilWyPainth.setStrokeWidth(10f);
        pmvilWyPainth.setStrokeCap(Paint.Cap.ROUND);
        pmvilWyPainth.setStyle(Paint.Style.STROKE);


        pmvilWyPaintw.setFlags(Paint.ANTI_ALIAS_FLAG);
        pmvilWyPaintw.setColor(Color.WHITE);
        pmvilWyPaintw.setStrokeWidth(10f);
        pmvilWyPaintw.setStrokeCap(Paint.Cap.ROUND);
        pmvilWyPaintw.setStyle(Paint.Style.STROKE);

        pmvilNyPaint1.setFlags(Paint.ANTI_ALIAS_FLAG);
        pmvilNyPaint1.setColor(Color.parseColor("#FF7E54"));
        pmvilNyPaint1.setStrokeWidth(10f);
        pmvilNyPaint1.setStrokeCap(Paint.Cap.ROUND);
        pmvilNyPaint1.setStyle(Paint.Style.FILL);

        pmvilNyPaint2.setFlags(Paint.ANTI_ALIAS_FLAG);
        pmvilNyPaint2.setColor(Color.parseColor("#F85B40"));
        pmvilNyPaint2.setStrokeWidth(10f);
        pmvilNyPaint2.setStrokeCap(Paint.Cap.ROUND);
        pmvilNyPaint2.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int pmvilwidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int pmvilheightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (pmvilwidthMode == MeasureSpec.AT_MOST && pmvilheightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        } else if (pmvilwidthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(DEFAULT_WIDTH, heightMeasureSpec);
        } else if (pmvilheightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthMeasureSpec, DEFAULT_HEIGHT);
        }
    }

    @Override
    protected void onDraw(Canvas pmvilcanvas) {
        super.onDraw(pmvilcanvas);
        int pmvilCenterX = getWidth()/2;
        int pmvilCenterY = getHeight()/2;

        float pmvilRadius = Math.min(pmvilCenterX, pmvilCenterY) - pmvilWyPainth.getStrokeWidth();

        pmvilcanvas.drawCircle(pmvilCenterX, pmvilCenterY, pmvilRadius, pmvilWyPainth);
        pmvilcanvas.drawCircle(pmvilCenterX, pmvilCenterY, pmvilRadius-15, pmvilNyPaint1);
        pmvilcanvas.drawCircle(pmvilCenterX, pmvilCenterY, pmvilRadius-30, pmvilNyPaint2);

        pmvilcanvas.drawArc(pmvilCenterX - pmvilRadius,pmvilCenterY-pmvilRadius,pmvilCenterX + pmvilRadius,pmvilCenterY + pmvilRadius,270, _angle,false, pmvilWyPaintw);



    }

    public void setPmvilProgress(int pmvilprogress){
        ValueAnimator pmvilAnimator = ValueAnimator.ofFloat(0,100f);
        Map<String, Object> pmvilAnimos = new HashMap<>();
        pmvilAnimator.addUpdateListener(animation -> {
            float cur = (float) animation.getAnimatedValue();
            _angle = cur/100 * 360 * pmvilprogress/100;
            pmvilAnimos.put("pmvilse",_angle);
            invalidate();
            if(pmvilLimitSuccess!=null){
                pmvilLimitSuccess.pmvilNowAngle(_angle);
                pmvilAnimos.put("pmvilse",_angle);
            }else {
                pmvilAnimos.put("pmvilse",_angle);
            }
        });
        pmvilAnimator.setDuration(3000);
        pmvilAnimator.setInterpolator(new AccelerateInterpolator());
        pmvilAnimator.start();
    }

    public interface PmvilLimitSuccess{
        void pmvilNowAngle(float pmvilAngle);
    }

    private PmvilLimitSuccess pmvilLimitSuccess;

    public void setPmvilLimitSuccess(PmvilLimitSuccess pmvilLimitSuccess) {
        this.pmvilLimitSuccess = pmvilLimitSuccess;
    }
}
